module.exports = app => {
  const mongoose = app.mongoose,
        Schema = mongoose.Schema,
        ObjectId = Schema.Types.ObjectId;
  /**
   * @ comment data schema
   */
  let CommentSchema = new Schema({
    toArticle: { type: ObjectId, ref: 'Article' },  // 文章ID
    author: { type: ObjectId, ref: 'Member' },  // 当前评论者ID
    reply: [{ type: ObjectId, ref: 'Reply' }],
    content: {type: String, default: ''},
    date: { type: Date, default: Date.now() }
  }, {usePushEach: true});
  CommentSchema.statics = {
    add(data) {
      console.log('22.....', data);
      return data.save();
    },
    update(params, cb) {
      return this.findByIdAndUpdate({_id: params.toComment}, {$push: {reply: params.rid}}, {new: true}, cb);
    },
    findAll(aid, cb) {
      return this.find({toArticle: aid})
        .populate('author', {'_id':1,'username':1,'avatarsPath':1})
        // .populate('toAuthor', {'_id': 1,'username':1,'avatarsPath':1})
        .populate({path:'reply',populate:[{path:'author',select:{'_id':1,'username':1,'avatarsPath':1}},{path:'toAuthor',select:{'_id':1,'username':1,'avatarsPath':1}}]})
        .exec(cb);
    },
    findById: function(id, cb) {
      return this.findOne({_id: id}).exec(cb);
    },
    findAllReply(cid, cb) {
      return this.find({toComment: cid})
        .populate('author', {'_id':1,'username':1,'avatarsPath':1})
        // .populate('toAuthor', {'_id': 1,'username':1,'avatarsPath':1})
        // .populate({
        //   path:'reply',
        //   populate:[{
        //     path:'author',
        //     select:{'_id':1,'username':1,'avatarsPath':1}
        //   }, {
        //     path:'toAuthor',
        //     select:{'_id':1,'username':1,'avatarsPath':1}
        //   }]
        // })
        .exec(cb);
    }
  };
  
  return mongoose.model('Comment', CommentSchema);
}





// const mongoose = require('mongoose');
// const CommentSchema = require('../schemas/comment');
// const Comment = mongoose.model('Comment', CommentSchema);

// module.exports = Comment;